package com.lry.crawlers.comic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.crawlers.comic.entity.Comic;
import com.lry.crawlers.comic.mapper.ComicMapper;
import com.lry.crawlers.comic.service.ComicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ComicServiceImpl implements ComicService {
    @Autowired
    ComicMapper comicMapper;

    @Override
    public int setComic(Comic comic) {
        int insert = comicMapper.insert(comic);
        return insert;
    }


    @Override
    public int setComics(List<Comic> comics) {
        int i = comicMapper.insertBatchSomeColumn(comics);
        return i;
    }



    @Override
    public List<Integer> removeDuplicates(List<String> titles) {
        List<Integer> duplicates = new ArrayList<>();
        for(int i = 0;i<titles.size();i++){
            QueryWrapper wrapper  = new QueryWrapper();
            wrapper.eq("title",titles.get(i));
            Comic f = comicMapper.selectOne(wrapper);
            if(f!=null){
                duplicates.add(i);
            }
        }
        return  duplicates;
    }

    @Override
    public void setupdateTime(Comic comic) {
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("comic_id",comic.getComicId());
        comicMapper.update(comic,wrapper);
    }

    @Override
    public Boolean removeDuplicate(String title) {
        QueryWrapper wrapper  = new QueryWrapper();
        wrapper.eq("title",title);
        Comic f = comicMapper.selectOne(wrapper);
        return f==null ? false : true;
    }

}


